package com.example.weibo_suhuanzhen.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.weibo_suhuanzhen.homeRecycle.ImageViewHolder;
import com.example.weibo_suhuanzhen.homeRecycle.VideoViewHolder;
import com.example.weibo_suhuanzhen.R;
import com.example.weibo_suhuanzhen.bean.WeiboInfo;
import com.example.weibo_suhuanzhen.sharedPreferences.SharedPreferencesManager;


import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<WeiboInfo> itemList;
    private Context context;
    private static final int TYPE_TEXT = 0;
    private static final int TYPE_VIDEO = 1;
    private static final int TYPE_IMAGE = 2;
    private ItemActionListener itemActionListener;
    final String TAG="shz_MyAdapter";
    private SharedPreferencesManager preferences;
    public interface ItemActionListener {
        void onLikeClicked(int position);
        void onCommentClicked(int position);
    }


    public MyAdapter(Context context, List<WeiboInfo> itemList) {
        this.context = context;
        this.itemList = itemList;
        this.preferences = SharedPreferencesManager.getInstance(context.getApplicationContext());
        this.itemActionListener = itemActionListener;
    }
    @Override
    public int getItemViewType(int position) {
        WeiboInfo item = itemList.get(position);
        if (item.getVideoUrl() != null && !item.getVideoUrl().isEmpty()) {
            return TYPE_VIDEO;
        } else if (item.getImages() != null && !item.getImages().isEmpty()) {
            return TYPE_IMAGE;
        } else {
            return TYPE_TEXT;
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_VIDEO) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_video, parent, false);
            return new VideoViewHolder(view, context,this);
        } else {
            View view = LayoutInflater.from(context).inflate(R.layout.item_image, parent, false);
            return new ImageViewHolder(view, context,this);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ImageViewHolder) {
            ((ImageViewHolder) holder).bind(itemList.get(position), itemActionListener, position);
        } else if (holder instanceof VideoViewHolder) {
            ((VideoViewHolder) holder).bind(itemList.get(position), itemActionListener, position);
        }
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }
    public void removeItem(int position) {
        if (position >= 0 && position < itemList.size()) {
            itemList.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, itemList.size());
        }
    }

    @Override
    public void onViewAttachedToWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        // Do nothing, video will not auto play
    }

    @Override
    public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        if (holder instanceof VideoViewHolder) {
            VideoViewHolder videoViewHolder = (VideoViewHolder) holder;
            if (videoViewHolder.isPlaying) {
                videoViewHolder.mediaPlayer.pause();
                videoViewHolder.isPlaying = false;
                videoViewHolder.playBtn.setVisibility(View.VISIBLE);
                videoViewHolder.thumbnail.setVisibility(View.VISIBLE);
            }
        }
    }
}